﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Threading;
using System.Text.RegularExpressions;
using UDP_WPF;
/// <summary>
/// 数据处理线程
/// </summary>

class DataHandler
{
    private delegate void RecDelegate(string str, int ActionStyle); //委托

    public string AcceptData;    //接收到的数据
    public IPEndPoint receivepoint; //目标地址
    Thread thread_rec;      //数据处理线程
   
    public DataHandler(string data, IPEndPoint rec)
    {
        AcceptData = data;
        receivepoint = rec;

        thread_rec = new Thread(unpack_thread);
        thread_rec.IsBackground = true;
        thread_rec.Start();
    }

    //数据处理线程
    public void unpack_thread()
    {
        unpack(AcceptData, receivepoint);
    }

    //解包处理
    public void unpack(string strData, IPEndPoint receivepoint)
    {
        string[] str = strData.Split(NetPara.PACKET_SPLITE_CHAR);  //获得包类型
        fresh("连接 - 来源：" + receivepoint.Address.ToString() + "  内容：" + strData, 2);    //界面输出

        /// <summary>
        /// 类型 - 连接 "str[0] = gconnect"
        /// </summary>
        if (NetPacket.PACTET_MSGSTYLE_NET_CONNECT == str[0])   
        {
            IPEndPoint iep = new IPEndPoint(receivepoint.Address, NetPara.PORT_CLIENT);   //客户端IP地址
            NetPara.IPE_CLIENT = iep;     //修改默认客户端IP地址
            DataSend.Instance().send_PACTET_MSGSTYLE_NET_CONNECT(NetPara.IPE_CLIENT);   //发送 - 返回连接响应消息
        }

        /// <summary>
        /// 类型 - 客户端关闭 "str[0] = gclose"
        /// </summary>
        else if (NetPacket.PACTET_MSGSTYLE_CLOSE == str[0])  
        {
            //把所有按下去的按键弹起
            GeneralPara gp = new GeneralPara();
            gp.GetAllKey();
            for (int i = 0; i < gp.downingKey.Count; i++)
                Keyevent.Instance().KeyUp((byte)gp.downingKey[i]);    //合法发送弹起按键消息
        }

        /// <summary>
        /// 类型 - 按钮点击 "str[0] = kc"
        /// </summary>
        else if (NetPacket.PACTET_ACTION_KEY_CLICK == str[0])
        {
            //if (IsKeyMsgLegal(str))
            //{
            //    if(NetPacket.PACTET_ACTION1.Equals(str[1]))
            //    {
            //        Keyevent.Instance().KeyClick((byte)GeneralPara.ACTION1_TO_KEY);    //合法发送按键消息
            //    }
            //    if (NetPacket.PACTET_ACTION2.Equals(str[1]))
            //    {
            //        Keyevent.Instance().KeyClick((byte)GeneralPara.ACTION2_TO_KEY);    //合法发送按键消息
            //    }
            //    if (NetPacket.PACTET_ACTION3.Equals(str[1]))
            //    {
            //        Keyevent.Instance().KeyClick((byte)GeneralPara.ACTION3_TO_KEY);    //合法发送按键消息
            //    }
            //    if (NetPacket.PACTET_ACTION4.Equals(str[1]))
            //    {
            //        Keyevent.Instance().KeyClick((byte)GeneralPara.ACTION4_TO_KEY);    //合法发送按键消息
            //        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION4_TO_KEY);    //合法发送按键消息
            //    }
            //}
        }
       
        /// <summary>
        /// 类型 - 按钮按下   str[0] == "kd"
        /// </summary>
        else if (NetPacket.PACTET_ACTION_KEY_DOWN == str[0])
        {
            if (IsKeyMsgLegal(str))
            {
                if (NetPacket.PACTET_ACTION_SENSOR_YU.Equals(str[1]))   //上的按下
               {
                   Keyevent.Instance().KeyDown((byte)GeneralPara.ACTION_SENSOR_YU_TO_KEY);    //合法发送按键消息
               }
                if (NetPacket.PACTET_ACTION_SENSOR_YD.Equals(str[1]))   //下的按下
               {
                   Keyevent.Instance().KeyDown((byte)GeneralPara.ACTION_SENSOR_YD_TO_KEY);    //合法发送按键消息
               }
                if (NetPacket.PACTET_ACTION_SENSOR_XLEFT.Equals(str[1]))   //左的按下
               {
                   Keyevent.Instance().KeyDown((byte)GeneralPara.ACTION_SENSOR_XLEFT_TO_KEY);    //合法发送按键消息
               }
                if (NetPacket.PACTET_ACTION_SENSOR_XRIGHT.Equals(str[1]))   //右的按下
               {
                   Keyevent.Instance().KeyDown((byte)GeneralPara.ACTION_SENSOR_XRIGHT_TO_KEY);    //合法发送按键消息
               }

   
                    if (NetPacket.PACTET_ACTION1.Equals(str[1]))
                    {
                        Keyevent.Instance().KeyDown((byte)GeneralPara.ACTION1_TO_KEY);    //合法发送按键消息
                        //Thread.Sleep(1100);
                        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION1_TO_KEY);    //合法发送按键消息
                    }
                    if (NetPacket.PACTET_ACTION2.Equals(str[1]))
                    {
                        Keyevent.Instance().KeyDown((byte)GeneralPara.ACTION2_TO_KEY);    //合法发送按键消息
                        //Thread.Sleep(220);
                        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION2_TO_KEY);    //合法发送按键消息
                    }
                    if (NetPacket.PACTET_ACTION3.Equals(str[1]))
                    {
                        Keyevent.Instance().KeyDown((byte)GeneralPara.ACTION3_TO_KEY);    //合法发送按键消息
                        //Thread.Sleep(220);
                        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION3_TO_KEY);    //合法发送按键消息
                    }
                    if (NetPacket.PACTET_ACTION4.Equals(str[1]))
                    {
                        Keyevent.Instance().KeyDown((byte)GeneralPara.ACTION4_TO_KEY);    //合法发送按键消息
                        //Thread.Sleep(520);
                        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION4_TO_KEY);    //合法发送按键消息
                        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION4_TO_KEY);    //合法发送按键消息
                    }
            
            }
                
        }

        /// <summary>
        /// 类型 - 按钮弹起   str[0] == "ku"
        /// </summary>
        else if (NetPacket.PACTET_ACTION_KEY_UP == str[0])   
        {
            if (IsKeyMsgLegal(str))
            {
                if (NetPacket.PACTET_ACTION_SENSOR_YMIDDLE.Equals(str[1]))   //上的弹起
                {
                    Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION_SENSOR_YU_TO_KEY);    //合法发送按键消息
                    Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION_SENSOR_YD_TO_KEY);    //合法发送按键消息
                }
                if (NetPacket.PACTET_ACTION_SENSOR_XMIDDLE.Equals(str[1]))   //下的弹起
                {
                    Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION_SENSOR_XLEFT_TO_KEY);    //合法发送按键消息
                    Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION_SENSOR_XRIGHT_TO_KEY);    //合法发送按键消息
                }
                  if (NetPacket.PACTET_ACTION1.Equals(str[1]))
                    {
                        Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION1_TO_KEY);    //合法发送按键消息
                        //Thread.Sleep(1100);
                        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION1_TO_KEY);    //合法发送按键消息
                    }
                    if (NetPacket.PACTET_ACTION2.Equals(str[1]))
                    {
                        Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION2_TO_KEY);    //合法发送按键消息
                        //Thread.Sleep(220);
                        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION2_TO_KEY);    //合法发送按键消息
                    }
                    if (NetPacket.PACTET_ACTION3.Equals(str[1]))
                    {
                        Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION3_TO_KEY);    //合法发送按键消息
                        //Thread.Sleep(220);
                        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION3_TO_KEY);    //合法发送按键消息
                    }
                    if (NetPacket.PACTET_ACTION4.Equals(str[1]))
                    {
                        Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION4_TO_KEY);    //合法发送按键消息
                        //Thread.Sleep(520);
                        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION4_TO_KEY);    //合法发送按键消息
                        //Keyevent.Instance().KeyUp((byte)GeneralPara.ACTION4_TO_KEY);    //合法发送按键消息
                    }
            }
        }
    }

    /// <summary>
    /// 功能函数
    /// </summmary>
    //界面交互委托刷新方法
    public void fresh(string str, int ActionStyle)
    {
        //状态刷新
        switch(ActionStyle)
        {
            case 1:
                if (Driver.static_driver.clientippaddr.InvokeRequired)//等待异步
                {
                    RecDelegate fc = new RecDelegate(fresh);
                    Driver.static_driver.Invoke(fc, str, ActionStyle); //通过代理调用刷新方法
                }
                else
                {
                    Driver.static_driver.clientippaddr.Text = str;
                }
                break;
            case 2:
                if (Driver.static_driver.tb_printftest.InvokeRequired)//等待异步
                {
                    RecDelegate fc = new RecDelegate(fresh);
                    Driver.static_driver.Invoke(fc, str, ActionStyle); //通过代理调用刷新方法
                }
                else
                {
                    Driver.static_driver.tb_printftest.Text = Driver.static_driver.tb_printftest.Text + "\r\n" + str;
                }
                break;
        }
    }

    //按钮消息是否合法
    public bool IsKeyMsgLegal(string [] str)
    {
        if (str.Length > 1)
        {
            //if (IsInteger(str[1]))
                return true;
        }
        return false;
    }
    //字符是否是整数
    public bool IsInteger(string str)
    {
        Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
        if (regex.IsMatch(str.Trim()))   
        {
            return true;   //合法 整数
        }
        return false;
    }
}